import 'package:flutter/material.dart';
import 'package:speech_to_text/speech_to_text.dart';
import 'package:ai_voice_chat/service/text_to_speech_service.dart';

void main() {
  runApp(const AiVoiceChatApp());
}

class AiVoiceChatApp extends StatelessWidget {
  const AiVoiceChatApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final SpeechToText _speech = SpeechToText();
  bool _isListening = false;
  String _text = '按按钮对话...';
  
  get ttsService => TextToSpeechService();

  Future<void> _listen() async {
    _text = "请按按钮对话...";
    if (!_isListening) {
      bool available = await _speech.initialize();
      if (available) {
        _text = "输入中...";
        setState(() => _isListening = true);
        _speech.listen(
          onResult: (result) {
            setState(() {
              print(result.recognizedWords); 
              _text = result.recognizedWords;
              // ttsService.speak(_text); // 识别到语音后进行朗读
            });
          }
        );
      }
    } else {
      setState(() => _isListening = false);
      _speech.stop();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            colors: [Color(0xFFF8EAF6), Color(0xFFE1F5FE)],
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
          ),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Spacer(flex: 2),
            // 头像
            CircleAvatar(
              radius: 64,
              backgroundImage: AssetImage('assets/avatar.png'),
              backgroundColor: Colors.white,
            ),
            const SizedBox(height: 24),
            // 昵称/状态
            const Text(
              '助手小蜜',
              style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 8),
            Text(
              _text,
              style: const TextStyle(fontSize: 16, color: Colors.grey),
            ),
            const Spacer(flex: 3),
            // 底部按钮
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 32.0, vertical: 24),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  _CircleButton(icon: Icons.mic, onTap: _listen),
                  // 可扩展其他按钮
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class _CircleButton extends StatelessWidget {
  final IconData icon;
  final VoidCallback onTap;
  final Color color;

  const _CircleButton({
    Key? key,
    required this.icon,
    required this.onTap,
    this.color = Colors.black87,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: 56,
        height: 56,
        decoration: BoxDecoration(
          color: Colors.white,
          shape: BoxShape.circle,
          boxShadow: [
            BoxShadow(
              color: Colors.black12,
              blurRadius: 6,
              offset: Offset(0, 2),
            ),
          ],
        ),
        child: Icon(icon, color: color, size: 28),
      ),
    );
  }
}
